package com.xyht.medicine_management_system.modules.shoppingCart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.modules.medicine.entity.MmsMedicine;
import com.xyht.medicine_management_system.modules.medicine.entity.MmsMedicineSpecial;
import com.xyht.medicine_management_system.modules.medicine.entity.resp.MmsMedicineDetailResp;
import com.xyht.medicine_management_system.modules.medicine.mapper.MmsMedicineMapper;
import com.xyht.medicine_management_system.modules.medicine.mapper.MmsMedicineSpecialMapper;
import com.xyht.medicine_management_system.modules.shoppingCart.entity.MmsShoppingCart;
import com.xyht.medicine_management_system.modules.shoppingCart.entity.req.MmsShoppingCartReq;
import com.xyht.medicine_management_system.modules.shoppingCart.entity.resp.MmsShoppingCartResp;
import com.xyht.medicine_management_system.modules.shoppingCart.mapper.MmsShoppingCartMapper;
import com.xyht.medicine_management_system.modules.shoppingCart.entity.MmsShoppingCart;
import com.xyht.medicine_management_system.modules.shoppingCart.entity.req.MmsShoppingCartReq;
import com.xyht.medicine_management_system.modules.shoppingCart.mapper.MmsShoppingCartMapper;
import com.xyht.medicine_management_system.modules.shoppingCart.service.MmsShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.medicine_management_system.modules.medicine.constant.MedicineIsSpecialConstant.MEDICINE_IS_NOT_SPECIAL;
import static com.xyht.medicine_management_system.modules.medicine.constant.MedicineIsSpecialConstant.MEDICINE_IS_SPECIAL;

/**
 * <p>
 * 活动表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-12-05
 */
@Service
public class MmsShoppingCartServiceImpl extends ServiceImpl<MmsShoppingCartMapper, MmsShoppingCart> implements MmsShoppingCartService {
    @Resource
    private MmsShoppingCartMapper mmsShoppingCartMapper;
    @Resource
    private MmsMedicineMapper mmsMedicineMapper;
    @Resource
    private MmsMedicineSpecialMapper mmsMedicineSpecialMapper;

    @Override
    public ResponseResult addShoppingCart(MmsShoppingCartReq shoppingCartReq, String userId) {
        if (isNullOrEmpty(shoppingCartReq.getMedicineId(), shoppingCartReq.getQuantity())) {
            if (shoppingCartReq.getQuantity() < 0) {
                return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
            }
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsMedicine medicine = mmsMedicineMapper.selectById(shoppingCartReq.getMedicineId());
        if (isNullOrEmpty(medicine)) {
            return CommonResult.failed(CommonCodeEnum.MEDICINE_NOT_EXIST);
        }

        //添加本购物车
        MmsShoppingCart shoppingCart = new MmsShoppingCart();
        shoppingCart.setUserId(userId);
        BeanUtils.copyProperties(shoppingCartReq, shoppingCart);
        mmsShoppingCartMapper.insert(shoppingCart);

        return CommonResult.success();
    }

    @Override
    public ResponseResult updateShoppingCart(MmsShoppingCartReq shoppingCartReq, String userId) {
        if (isNullOrEmpty(shoppingCartReq.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        MmsShoppingCart shoppingCart = mmsShoppingCartMapper.selectById(shoppingCartReq.getId());
        if (isNullOrEmpty(shoppingCart)) {
            return CommonResult.failed(CommonCodeEnum.SHOPPING_CART_NOT_EXIST);
        }

        MmsShoppingCart updShoppingCart = new MmsShoppingCart();
        BeanUtils.copyProperties(shoppingCartReq, updShoppingCart, "medicineId");
        updShoppingCart.setId(shoppingCart.getId());
        updShoppingCart.setUserId(userId);

        mmsShoppingCartMapper.updateById(updShoppingCart);

        return CommonResult.success();
    }

    @Override
    public ResponseResult listShoppingCart(Integer pageNum, Integer pageSize, String searchStr, String userId) {
        LambdaQueryWrapper<MmsShoppingCart> queryWrapper = new LambdaQueryWrapper<MmsShoppingCart>()
                .eq(MmsShoppingCart::getUserId, userId);

        if (!isNullOrEmpty(searchStr)) {
            StringUtils.deleteWhitespace(searchStr);//去除空格
            //药品名
            List<MmsMedicine> medicineList = mmsMedicineMapper.selectList(new LambdaQueryWrapper<MmsMedicine>()
                    .like(MmsMedicine::getCommonNames, searchStr));
            if (!medicineList.isEmpty()) {
                List<String> medicineIdList = medicineList.stream().map(MmsMedicine::getId).collect(Collectors.toList());
                queryWrapper.in(MmsShoppingCart::getMedicineId, medicineIdList);
            }
            //数量、备注
            queryWrapper.like(MmsShoppingCart::getQuantity, searchStr).or()
                    .like(MmsShoppingCart::getNote, searchStr);
        }

        Page<MmsShoppingCart> page = new Page<>(pageNum, pageSize);
        mmsShoppingCartMapper.selectPage(page, queryWrapper);

        List<MmsShoppingCartResp> shoppingCartRespList = page.getRecords().stream().map(shoppingCart -> {
            MmsShoppingCartResp shoppingCartResp = new MmsShoppingCartResp();
            BeanUtils.copyProperties(shoppingCart, shoppingCartResp);

            //设置药品信息
            MmsMedicine medicine = mmsMedicineMapper.selectById(shoppingCart.getMedicineId());
            MmsMedicineDetailResp medicineDetailResp = new MmsMedicineDetailResp();
            BeanUtils.copyProperties(medicine, medicineDetailResp);
            //判断是否特惠商品
            List<MmsMedicineSpecial> specialList = mmsMedicineSpecialMapper.selectList(new LambdaQueryWrapper<MmsMedicineSpecial>()
                    .eq(MmsMedicineSpecial::getMedicineId, medicine.getId()));
            if (specialList != null && specialList.size() > 0 ) {
                medicineDetailResp.setIsSpecial(MEDICINE_IS_SPECIAL);
                medicineDetailResp.setSpecialPrice(specialList.get(0).getSpecialPrice());
            } else {
                medicineDetailResp.setIsSpecial(MEDICINE_IS_NOT_SPECIAL);
            }
            shoppingCartResp.setMedicineDetailResp(medicineDetailResp);

            return shoppingCartResp;
        }).collect(Collectors.toList());

        return CommonResult.success(shoppingCartRespList, (int) page.getTotal());
    }

}
